import { ref } from 'vue'

const isDark = ref<boolean>(false)

function setDark(dark : boolean) {
	isDark.value = dark
	// #ifdef H5
	process.env.NODE_ENV === 'development' && uni.setStorageSync('isDark', dark)
	// #endif
	// #ifndef H5
	uni.setStorageSync('isDark', dark)
	// #endif
}

/**
 * 设置登录Token
 * @param {String} token 
 * @return 
 */
export function setToken(token : String) {
	uni.setStorageSync('token', token)
}

/**
 * 获取Token
 * @return 
 */
export function getToken() : String {
	return uni.getStorageSync<String>('token')
}

/**
 * 清除登录Token
 * @return 
 */
export function clearToken() {
	uni.removeStorageSync('token')
}

export function useDark() {
	// #ifdef H5
	process.env.NODE_ENV === 'development'
		? setDark(Boolean(uni.getStorageSync('isDark')))
		: setDark(localStorage.getItem('vitepress-theme-appearance') === 'dark')
	// #endif
	// #ifndef H5
	setDark(Boolean(uni.getStorageSync('isDark')))
	// #endif
	return { isDark, setDark }
}